<?php
include 'mysql.php';

if(isset($_SERVER['REQUEST_METHOD'])&&strtolower($_SERVER['REQUEST_METHOD'])=='post')
{
         $param = $_REQUEST['Form'] ;
         $title = $param['title'];
         $name = $param['name'];
         $mobile = $param['mobile'];
         $desc = $param['desc'];
         if($_FILES['cover'] )
         {
            $filename = $_FILES['cover']['type'];
            $newname = './upload/'.time().'.jpg';
            if(in_array(strtolower($filename),array('image/jpeg','image/png')))
            {
                if(!move_uploaded_file($_FILES['cover']['tmp_name'] ,$newname) )
                {
                     die ('图片上传失败');
                }
                else
                {
                     $query = "INSERT INTO  `activity` (`id`, `title`, `name`, `mobile`, `desc`, `cover`) VALUES (NULL, '".$title."', '".$name."', '".$mobile."', '".$desc."', '".$newname."')";

                    $result=$mysqli->query($query);
                    if(!$result)
                    {
                        echo  "<script>alert('添加失败')</script>";
                    }

                }
            }

         }


}



$activity_list = array() ;
$result = $mysqli->query("select * from activity");
while($row =$result->fetch_array() ){
    $activity_list[] = $row;
}

?>

<!doctype html>
<html>
<head>
    <title>后台管理</title>
    <meta charset="UTF-8">
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
    <link href="css/admin_full.css" rel="stylesheet" />
    <script src="js/jquery.js"></script>
    <script type="text/javascript">
		$(function(){

            $('form[name="form_submit"]').submit(function(){

                var title = $('#title').val();
                var name = $('#name').val();
                var mobile = $('#mobile').val();
                var cover = $('#cover').val();
                var desc = $('#desc').val();

                if(!title.length){
                    $('#title').focus();
                    return false ;
                }

                if(!name.length){
                    $('#name').focus();
                    return false ;
                }

                if(!mobile.length){
                    $('#mobile').focus();
                    return false ;
                }
                if(cover==""){
                    $('#cover').focus();
                    return false ;
                }
                if(!desc.length){
                    $('#desc').focus();
                    return false ;
                }

            });
        })

		</script>
</head>
<body>

<div id="popup"></div>
<div class="wrap J_check_wrap">
    <div class="h_a">功能说明</div>
    <div class="prompt_text">
       发布活动,管理活动
    </div>




    <div class="h_a">活动列表</div>
    <div class="table_list" id="DataBox">
            <table width="100%">
                <thead>
                <tr>
                    <td>活动ID</td>
                    <td>活动标题</td>
                    <td>活动描述</td>
                    <td>活动封面</td>
                    <td>联系人</td>
                    <td>手机号</td>
                    <td>编辑</td>
                  </tr>
                </thead>
                <?php if(!empty($activity_list)&&isset($activity_list)):?>
                    <?php foreach($activity_list as $val):?>
                        <tr>
                        <td> <?php echo @$val['id'];?></td>
                        <td> <?php echo @$val['title'];?></td>
                        <td> <?php echo @$val['desc'];?></td>
                        <td><img width="120" height="80" src="<?php echo @$val['cover'];?>"></td>
                        <td> <?php echo @$val['name'];?></td>
                        <td> <?php echo @$val['mobile'];?></td>
                        <td> <a href="action.php?action=delete&id=<?php echo $val['id'];?>">删除</a>|<a href="edit.php?id=<?php echo @$val['id'];?>">编辑</a></td>
                        </tr>
                    <?php endforeach;?>
                <?php endif;?>

             </table>
    </div>

    <div class="h_a">发布活动</div>
    <div class="search_type cc mb10">
        <form action=" " method="post" name="form_submit" enctype="multipart/form-data">
            <div class="ul_wrap">
                <label>活动标题：</label>
                <input type="text" class="input length_3" name="Form[title]" id="title" placeholder="必填">
             </div>
              <div class="ul_wrap">
                <label>发起人：</label>
                <input type="text" class="input length_3" name="Form[name]" id="name" placeholder="必填">
             </div>
             <div class="ul_wrap">
                <label>活动封面：</label>
                <input type="file" class="input length_3" name="cover" id="cover"  >
             </div>
            <div class="ul_wrap">
                <label>手机号：</label>
                <input type="text" class="input length_3" name="Form[mobile]" id="mobile"  placeholder="必填">
             </div>
            <div class="ul_wrap">
                <label>活动描述：</label>
                <textarea  name="Form[desc]"  placeholder="必填"  id="desc" style="width:300px;"></textarea>
             </div>
             <div class="ul_wrap"> <button class="btn" type="submit">确定</button> </div>
        </form>
    </div>
</div>
</body>
</html>

<?php $mysqli->close(); ?>